home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / DATDISP.ZIP / DATADISP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-30  |  10.9 KB  |  467 lines

  1. /*
  2.  
  3.     Program : DATADISP (Main)
  4.     Authors : W.E.R. Stroebel.
  5.     Purpose : Displaying an unknown file in a "readable" way.
  6.     Modules : None
  7.     Compile : TCC, large model, enable stack checking.
  8.     Created : June 29, 1989
  9.     Edited  : June 30, 1989
  10.               - Small problem with labels fixed.
  11.           - Unknowns > 16 bytes split.
  12.  
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <conio.h>
  17. #include <stdlib.h>
  18. #include <alloc.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21.  
  22. enum types {
  23.     UNKNOWN, ZSTR, FSTR, LSTR, UNS16, INT16, UNS32, INT32, FLOAT, DOUBLE
  24. };
  25.  
  26. struct items {
  27.     char label [4];
  28.     enum types type;
  29.     int len;
  30. } *item, ti;
  31.  
  32. union data {
  33.     char *s;
  34.     unsigned u;
  35.     int d;
  36.     long unsigned lu;
  37.     long ld;
  38.     float e;
  39.     double le;
  40. } d;
  41.  
  42. FILE *df, *cf;
  43. int numdef, maxitem;
  44.  
  45. void read_unknown (void)
  46. {
  47.     int cnt;
  48.     long siz, fsiz;
  49.  
  50.     fseek (df, 0, SEEK_END);
  51.     fsiz = ftell (df);
  52.     siz  = 0;
  53.     for (cnt = 0; siz < fsiz && cnt < maxitem; cnt ++) {
  54.     item [cnt].type = UNKNOWN;
  55.     if (fsiz - siz >= 16) {
  56.         item [cnt].len = 16;
  57.         siz += 16;
  58.     } else {
  59.         item [cnt].len = (int) (fsiz - siz);
  60.         siz = fsiz;
  61.     }
  62.     }
  63.     numdef = cnt;
  64. }
  65.  
  66. void mputch (char c)
  67. {
  68.     if (c > 31 && c != '\\') {
  69.     putch (c);
  70.     } else {
  71.     switch (c) {
  72.         case  0    : break;
  73.         case  '\n' : printf ("\\n"); break;
  74.         case  '\t' : printf ("\\t"); break;
  75.         case  '\v' : printf ("\\v"); break;
  76.         case  '\b' : printf ("\\b"); break;
  77.         case  '\r' : printf ("\\r"); break;
  78.         case  '\f' : printf ("\\f"); break;
  79.         case  '\a' : printf ("\\a"); break;
  80.         case  '\\' : printf ("\\\\"); break;
  81.         default    : printf ("\\x%02X", (unsigned char) c); break;
  82.     }
  83.     }
  84. }
  85.  
  86. void display (int inum)
  87. {
  88.     int cnt, c;
  89.     long offset;
  90.     char lab [5];
  91.  
  92.     offset = 0;
  93.     lab [4] = 0;
  94.     for (cnt = 0; cnt < inum; cnt ++) offset += item [cnt].len;
  95.     fseek (df, offset, SEEK_SET);
  96.     while (wherey () < 22 && cnt < numdef) {
  97.     if (item [cnt].label [0] == 0) {
  98.         printf ("%4u ", cnt);
  99.     } else {
  100.         memcpy (lab, item [cnt].label, 4);
  101.         printf ("%4s ", lab);
  102.     }
  103.     switch (item [cnt].type) {
  104.         case ZSTR :
  105.         printf ("Z%2d:", item [cnt].len);
  106.         d.s = (char *) malloc (item [cnt].len);
  107.         fread (d.s, item [cnt].len, 1, df);
  108.         putch ('"');
  109.         for (c = 0; d.s [c] != 0; c ++) {
  110.             mputch (d.s [c]);
  111.         }
  112.         printf ("\"\n");
  113.         free (d.s);
  114.         break;
  115.         case FSTR :
  116.         printf ("F%2d:", item [cnt].len);
  117.         d.s = (char *) malloc (item [cnt].len);
  118.         fread (d.s, item [cnt].len, 1, df);
  119.         putch ('"');
  120.         for (c = 0; c < item [cnt].len; c ++) {
  121.             mputch (d.s [c]);
  122.         }
  123.         printf ("\"\n");
  124.         free (d.s);
  125.         break;
  126.         case LSTR :
  127.         d.s = (char *) malloc (item [cnt].len);
  128.         fread (d.s, item [cnt].len, 1, df);
  129.         printf ("L%2d:", d.s [0]);
  130.         putch ('"');
  131.         for (c = 1; c <= d.s [0]; c ++) {
  132.             mputch (d.s [c]);
  133.         }
  134.         printf ("\"\n");
  135.         free (d.s);
  136.         break;
  137.         case UNS16 :
  138.         printf ("W%2d:", item [cnt].len);
  139.         fread (&d.u, item [cnt].len, 1, df);
  140.         printf ("%5u (%04X)\n", d.u, d.u);
  141.         break;
  142.         case INT16 :
  143.         printf ("I%2d:", item [cnt].len);
  144.         fread (&d.d, item [cnt].len, 1, df);
  145.         printf ("%5d (%04X)\n", d.d, d.d);
  146.         break;
  147.         case UNS32 :
  148.         printf ("D%2d:", item [cnt].len);
  149.         fread (&d.lu, item [cnt].len, 1, df);
  150.         printf ("%5lu (%08X)\n", d.lu, d.lu);
  151.         break;
  152.         case INT32 :
  153.         printf ("L%2d:", item [cnt].len);
  154.         fread (&d.ld, item [cnt].len, 1, df);
  155.         printf ("%5ld (%08X)\n", d.ld, d.ld);
  156.         break;
  157.         case FLOAT :
  158.         printf ("R%2d:", item [cnt].len);
  159.         fread (&d.e, item [cnt].len, 1, df);
  160.         printf ("%E\n", d.e);
  161.         break;
  162.         case DOUBLE :
  163.         printf ("D%2d:", item [cnt].len);
  164.         fread (&d.le, item [cnt].len, 1, df);
  165.         printf ("%lE\n", d.le);
  166.         break;
  167.         default :
  168.         printf ("U%2d:", item [cnt].len);
  169.         d.s = (char *) malloc (item [cnt].len);
  170.         fread (d.s, item [cnt].len, 1, df);
  171.         for (c = 0; c < item [cnt].len; c ++) {
  172.             printf ("%02X ", (unsigned char) d.s [c]);
  173.         }
  174.         gotoxy (64, wherey ());
  175.         for (c = 0; c < item [cnt].len; c ++) {
  176.             printf ("%c", d.s [c] > 31 ? d.s [c] : '.');
  177.         }
  178.         if (wherex () != 1) printf ("\n");
  179.         free (d.s);
  180.         break;
  181.     }
  182.     cnt ++;
  183.     }
  184. }
  185.  
  186. void delete (int inum)
  187. {
  188.     int c;
  189.  
  190.     for (c = inum; c < numdef - 1; c ++) {
  191.     item [c] = item [c + 1];
  192.     }
  193.     numdef --;
  194. }
  195.  
  196. void insert (int inum, struct items *i)
  197. {
  198.     int c;
  199.  
  200.     for (c = numdef; c > inum; c --) {
  201.     item [c] = item [c - 1];
  202.     }
  203.     item [inum] = *i;
  204.     if (numdef < maxitem) numdef ++;
  205. }
  206.  
  207. void cleanup (void)
  208. {
  209.     int cnt;
  210.     long siz, fsiz;
  211.     struct items t;
  212.     
  213.     fseek (df, 0, SEEK_END);
  214.     fsiz = ftell (df);
  215.     siz = 0;
  216.     for (cnt = 0; siz < fsiz && cnt < maxitem; cnt ++) {
  217.     if (item [cnt].type == UNKNOWN && item [cnt].len > 16) {
  218.         t.type = UNKNOWN;
  219.         t.len = item [cnt].len - 16;
  220.         t.label [0] = 0;
  221.         insert (cnt + 1, &t);
  222.         item [cnt].len = 16;
  223.         siz += 16;
  224.     } else {
  225.         siz += item [cnt].len;
  226.     }
  227.     }
  228.     numdef = cnt;
  229.     if (siz > fsiz) { /* last item is to big (and at least 2 bytes long) */
  230.     item [numdef - 1].len -= (int) (siz - fsiz);
  231.     if (item [numdef - 1].type != UNKNOWN) {
  232.         item [numdef - 1].type = UNKNOWN; /* regardless : wrong size ! */
  233.     }
  234.     }
  235. }
  236.  
  237. void add_item (int inum, struct items *i)
  238. {
  239.     struct items t;
  240.  
  241.     while (item [inum].len < i->len) {
  242.     item [inum].len += item [inum + 1].len;
  243.     delete (inum + 1);
  244.     }
  245.     if (item [inum].len > i->len) {
  246.     t.type = UNKNOWN;
  247.     t.len = item [inum].len - i->len;
  248.     t.label [0] = 0;
  249.     insert (inum + 1, &t);
  250.     item [inum] = *i;
  251.     } else {
  252.     item [inum].type = i->type;
  253.     }
  254.     /* cleanup : break up unknowns > 16 bytes, check file size */
  255.     cleanup ();
  256. }
  257.  
  258. void fixed_records (int fnum, int lnum)
  259. {
  260.     int cnt, des;
  261.     long siz, fsiz;
  262.  
  263.     lnum ++;
  264.     des = lnum;
  265.     fseek (df, 0, SEEK_END);
  266.     fsiz = ftell (df);
  267.     siz = 0;
  268.     for (cnt = 0; cnt < lnum; cnt ++) { /* header and first record size */
  269.     siz += item [cnt].len;
  270.     }
  271.     while (siz < fsiz && cnt < maxitem) {
  272.     for (cnt = fnum; cnt < lnum; cnt ++) {
  273.         item [des] = item [cnt];
  274.         siz += item [cnt].len;
  275.         des ++;
  276.     }
  277.     }
  278.     numdef = des;
  279.     cleanup (); /* in case user messed up defining record */
  280. }
  281.  
  282. main (int argn, char *argv [])
  283. {
  284.     int ch, inum, lnum, c2;
  285.     char s [80];
  286.  
  287.     clrscr ();
  288.     printf ("DATADISP - General datafile display program.\n\n");
  289.     if (argn < 2 || argn > 3 || strchr (argv [1], '?') != NULL) {
  290.     printf ("USAGE : DATADISP datafile <infofile>\n");
  291.     exit (1);
  292.     }
  293.     if ((df = fopen (argv [1], "rb")) == NULL) {
  294.     perror (argv [1]);
  295.     exit (1);
  296.     }
  297.     printf ("Opened data file : %s\n", argv [1]);
  298.     maxitem = 65000 / sizeof (struct items);
  299.     item = (struct items *) malloc (maxitem * sizeof (struct items));
  300.     memset (item, 0, maxitem * sizeof (struct items));
  301.     memset (&ti, 0, sizeof (ti));
  302.     if (argn == 3) {
  303.     printf ("Opened info file : %s", argv [2]);
  304.     if ((cf = fopen (argv [2], "rb")) == NULL) {
  305.         numdef = 0;
  306.         if ((cf = fopen (argv [2], "wb")) == NULL) {
  307.         perror (argv [2]);
  308.         exit (1);
  309.         }
  310.     } else {
  311.         numdef = fread (item, sizeof (struct items), maxitem, cf);
  312.     }
  313.     printf (" - Read %d item descriptors.\n", numdef);
  314.     fclose (cf);
  315.     unlink (argv [2]);
  316.     cf = fopen (argv [2], "wb");
  317.     } else {
  318.     cf = NULL;
  319.     }
  320.     if (numdef == 0) {
  321.     read_unknown ();
  322.     }
  323.     inum = 0;
  324.     printf ("Hit any key to start : ");
  325.     getch ();
  326.     do {
  327.     window (1, 1, 80, 22);
  328.     clrscr ();
  329.     display (inum);
  330.     window (1, 23, 80, 25);
  331.     clrscr ();
  332.     printf ("----------------------------------------");
  333.     printf ("----------------------------------------");
  334.     printf ("ITEM : Add, Remove, Display, Label. Fixed. Quit : ");
  335.     ch = getch ();
  336.     ch = toupper (ch);
  337.     switch (ch) {
  338.         case 'A' :
  339.         gotoxy (1, 2);
  340.         clreol ();
  341.         printf ("Enter item number : ");
  342.         scanf ("%d", &inum);
  343.         if (inum < numdef) {
  344.             gotoxy (1, 2);
  345.             clreol ();
  346.             printf ("TYPE : Unknown, String, Decimal, Real. Quit : ");
  347.             c2 = getch ();
  348.             c2 = toupper (c2);
  349.             switch (c2) {
  350.             case 'U' : ti.type = UNKNOWN; break;
  351.             case 'S' :
  352.                 gotoxy (1, 2);
  353.                 clreol ();
  354.                 printf ("STRING : Fixed, Asciiz, Length. Quit : ");
  355.                 c2 = getch ();
  356.                 c2 = toupper (c2);
  357.                 switch (c2) {
  358.                 case 'F' : ti.type = FSTR; break;
  359.                 case 'A' : ti.type = ZSTR; break;
  360.                 case 'L' : ti.type = LSTR; c2 = 'T'; break;
  361.                 default  : c2 = 'Q';
  362.                 }
  363.             break;
  364.             case 'D' :
  365.                 gotoxy (1, 2);
  366.                 clreol ();
  367.                 printf ("DECIMAL : Word, Int, Dword, Long. Quit : ");
  368.                 c2 = getch ();
  369.                 c2 = toupper (c2);
  370.                 switch (c2) {
  371.                 case 'W' : ti.type = UNS16; ti.len = 2; break;
  372.                 case 'I' : ti.type = INT16; ti.len = 2; break;
  373.                 case 'D' : 
  374.                     ti.type = UNS32; ti.len = 4; c2 = 'X';
  375.                 break;
  376.                 case 'L' : ti.type = INT32; ti.len = 4; break;
  377.                 default  : c2 = 'Q';
  378.                 }
  379.             break;
  380.             case 'R' :
  381.                 gotoxy (1, 2);
  382.                 clreol ();
  383.                 printf ("REAL : Single or Double precision. Quit : ");
  384.                 c2 = getch ();
  385.                 c2 = toupper (c2);
  386.                 switch (c2) {
  387.                 case 'S' : 
  388.                     ti.type = FLOAT; ti.len = sizeof (float);
  389.                 break;
  390.                 case 'D' :
  391.                     ti.type = DOUBLE; ti.len = sizeof (double);
  392.                 break;
  393.                 default  : c2 = 'Q';
  394.                 }
  395.             break;
  396.             }
  397.             if (strchr ("UFAT", c2) != NULL) {
  398.             gotoxy (1, 2);
  399.             clreol ();
  400.             printf ("Length : ");
  401.             scanf ("%d", &(ti.len));
  402.             }
  403.             if (c2 != 'Q') add_item (inum, &ti);
  404.         } else {
  405.             inum = 0;
  406.         }
  407.         break;
  408.         case 'R' :
  409.         gotoxy (1, 2);
  410.         clreol ();
  411.         printf ("Enter item number : ");
  412.         scanf ("%d", &inum);
  413.         if (inum < numdef - 1) {
  414.             item [inum].type = UNKNOWN;
  415.             item [inum].len += item [inum + 1].len;
  416.             delete (inum + 1);
  417.         } else {
  418.             inum = 0;
  419.         }
  420.         cleanup (); /* items > 16 bytes, filesize test */
  421.         break;
  422.         case 'D' :
  423.         gotoxy (1, 2);
  424.         clreol ();
  425.         printf ("Enter item number : ");
  426.         scanf ("%d", &inum);
  427.         if (inum < numdef) {
  428.         } else {
  429.             inum = 0;
  430.         }
  431.         break;
  432.         case 'L' :
  433.         gotoxy (1, 2);
  434.         clreol ();
  435.         printf ("Enter item number : ");
  436.         scanf ("%d", &inum);
  437.         if (inum < numdef) {
  438.             gotoxy (1, 2);
  439.             clreol ();
  440.             printf ("Enter item label (max 4 chars) : ");
  441.             scanf ("%s", s);
  442.             memcpy (item [inum].label, s, 4);
  443.         } else {
  444.             inum = 0;
  445.         }
  446.         break;
  447.         case 'F' :
  448.         gotoxy (1, 2);
  449.         clreol ();
  450.         printf ("Enter first item of fixed records : ");
  451.         scanf ("%d", &inum);
  452.         gotoxy (1, 2);
  453.         clreol ();
  454.         printf ("Enter last item of fixed records : ");
  455.         scanf ("%d", &lnum);
  456.         fixed_records (inum, lnum);
  457.         break;
  458.     }
  459.     } while (ch != 'Q');
  460.     fclose (df);
  461.     if (cf != NULL) {
  462.     fwrite (item, sizeof (struct items), numdef, cf);
  463.     fclose (cf);
  464.     }
  465.     return 0;
  466. }
  467.